from django.shortcuts import render
from rest_framework import request
from rest_framework.response import Response
from rest_framework.views import APIView
# Create your views here.
from users.models import User
from users.serializers import RegisterUserSerializer


class RegisterUsernameAPIView(APIView):

    def get(self, request, username):

        count = User.objects.filter(username=username).count()

        return Response({'count': count,
                         'username': username})

#
# class RegisterPhoneCountAPIView(APIView):
#     """
#     查询手机号的个数
#     GET: /users/phones/(?P<mobile>1[345789]\d{9})/count/
#     """
#     def get(self,request,mobile):
#
#         #通过模型查询获取手机号个数
#         count = User.objects.filter(mobile=mobile).count()
#         #组织数据
#         context = {
#             'count': count,
#             'phone': mobile
#         }
#
#         return Response(context)


class RegisterUserAPIView(APIView):
    def post(self, request):
        data = request.data
        serializer = RegisterUserSerializer(data=data)
        serializer.is_valid(raise_exception=True)
        serializer.save()


        return Response(serializer.data)

#自动登入

